package com.weseal.service.impl;

import com.weseal.dao.UserInfoDao;
import com.weseal.domain.UserInfo;
import com.weseal.enums.DateType;
import com.weseal.exception.UserException;
import com.weseal.form.BlacklistForm;
import com.weseal.form.UserInfoForm;
import com.weseal.query.FreedomUserQuery;
import com.weseal.query.UserInfoQuery;
import com.weseal.response.ResponseResult;

import com.weseal.service.UserInfoService;
import com.weseal.vo.UserInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @author weseal
 * @title
 * @description
 * @usage
 * @copyright Copyright 2014  rrmj Corporation. All rights reserved.
 * @company 上海众多美网络科技有限公司
 * @create 2017/9/6
 */
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {

	//@Autowired
	private UserInfoDao userInfoDao;

	//@Autowired
	//private UserDateInfoService  userDateInfoService;
	
	//@Autowired
	//private BlacklistService blacklistService;

	
	public int deleteByPrimaryKey(Long id) {
		return userInfoDao.deleteByPrimaryKey(id);
	}

	@Transactional
	@Override
	public int insert(UserInfoForm form) {
		if(form == null){
			throw  UserException.USER_INFO_EXCEPTION;
		}
		UserInfo userInfo = form.convert2Domain();
		int result = userInfoDao.insert(userInfo);
		//userDateInfoService.insert(userInfo.getId(), form.getPartTimes());
		return result;
	}
	
	@Override
	public void update(UserInfoForm form) {
		if (form == null) {
			throw UserException.USER_INFO_EXCEPTION;
		}
		UserInfo userInfo = form.convert2Domain();
		userInfoDao.updateByPrimaryKeySelective(userInfo);
		//userDateInfoService.insert(userInfo.getId(), form.getPartTimes());
	}
	
	@Override
	public ResponseResult<UserInfoVo> pageQuery(UserInfoQuery query) {
		List<UserInfo> userInfoList = userInfoDao.pageQuery(query);
		List<UserInfoVo> userInfoVos = UserInfoVo.convert2Vo(userInfoList);
		if(!CollectionUtils.isEmpty(userInfoVos)){
			for (UserInfoVo userInfoVo: userInfoVos) {
				//List<DateType> userDateInfos = userDateInfoService.listDateType(userInfoVo.getId());
			}
		}
		//return new ResponseResult<UserInfoVo>(userInfoVos,query,userInfoList);
		return null;
	}


	@Override
	public ResponseResult<UserInfoVo> pageQueryRemoveUserByTime(FreedomUserQuery query) {
		List<UserInfo> userInfoList = userInfoDao.pageQueryRemoveUserByTime(query);
		List<UserInfoVo> userInfoVos = UserInfoVo.convert2Vo(userInfoList);
		if (!CollectionUtils.isEmpty(userInfoVos)) {
			for (UserInfoVo userInfoVo : userInfoVos) {
				//List<DateType> userDateInfos = userDateInfoService.listDateType(userInfoVo.getId());
			}
		}
		return new ResponseResult<UserInfoVo>(userInfoVos, query, userInfoList);
	}


	
	
	@Override
	public UserInfoVo select(Long userId) {
		UserInfo userInfo = userInfoDao.selectByPrimaryKey(userId);
		//List<DateType> userDateInfos = userDateInfoService.listDateType(userInfo.getId());
		UserInfoVo userInfoVo = UserInfoVo.convert2Vo(userInfo);
		return userInfoVo;
	}
	
	@Transactional
	@Override
	public void addBlack(BlacklistForm form) {
		if (form == null || form.getUserId() == null) {
			throw UserException.USER_INFO_EXCEPTION;
		}
		UserInfo userInfo = userInfoDao.selectByPrimaryKey(form.getUserId());
		if (userInfo != null) {
			//blacklistService.save(form);
			userInfo.setBlack(Boolean.TRUE);
			userInfoDao.updateByPrimaryKeySelective(userInfo);
		}
	}


	@Transactional
	@Override
	public void removeBlack(BlacklistForm form) {
		if (form == null || form.getUserId() == null) {
			throw UserException.USER_INFO_EXCEPTION;
		}
		UserInfo userInfo = userInfoDao.selectByPrimaryKey(form.getUserId());
		if (userInfo != null) {
			form.setTerminalTime(new Date());
			//blacklistService.save(form);
			userInfo.setBlack(Boolean.FALSE);
			userInfoDao.updateByPrimaryKeySelective(userInfo);
		}
	}


	@Override
	public void delete(BlacklistForm form) {
		if (form == null || form.getUserId() == null) {
			throw UserException.USER_INFO_EXCEPTION;
		}
		userInfoDao.deleteByPrimaryKey(form.getUserId());
	}
}
